package com.gonsin.ta.logic.db.mapper;

import com.gonsin.ta.logic.db.SignalBean;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SignalMapper {

    /**
     * 插入信号
     */
    @Insert("insert into ta_chain_signal set " +
            "signal_id = #{bean.signalId}," +
            "instance_id = #{bean.instanceId}," +
            "gs_signal = #{bean.signal}," +
            "timeout = #{bean.timeout}," +
            "stack = #{bean.stack}," +
            "insert_time = #{bean.insertTime}," +
            "next = #{bean.next}," +
            "timeout_task = #{bean.timeoutTask}," +
            "context = #{bean.context}" +
            " ")
    void insertSignal(@Param("bean") SignalBean bean);


    /**
     * 查询信号
     */
    @Results(
            id = "SignalBean",
            value = {
                    @Result(column = "gs_signal", property = "signal"),
                    @Result(column = "instance_id", property = "instanceId"),
                    @Result(column = "signal_id", property = "signalId"),
                    @Result(column = "stack", property = "stack"),
                    @Result(column = "timeout_task", property = "timeoutTask"),
                    @Result(column = "insert_time", property = "insertTime"),
            }
    )
    @Select("select * from ta_chain_signal where instance_id = #{instanceId} and gs_signal = #{signal}")
    SignalBean selectSignal(@Param("instanceId") String instanceId, @Param("signal") String signal);


    /**
     * 删除信号
     */
    @Delete("delete from ta_chain_signal where signal_id = #{signalId}")
    void deleteSignal(@Param("signalId") String signalId);

    /**
     * 查询有timeout的信号任务
     */
    @ResultMap("SignalBean")
    @Select("select * from ta_chain_signal where timeout is not null and timeout_task is not null")
    List<SignalBean> selectAllTimeout();

}
